const express = require('express')
const expressJWT = require('express-jwt')
const JWT = require('./util/jwt')
const util = require('./util/util')
const errorVerify =  require('./middle/error')
const filterVerify =  require('./middle/filter')
const sysConfig = require('./config/sysConfig')
const routeConfig = require('./config/routeConfig')
const app = express()
const port = 3000
// 跨域
const cors = require('cors')
// 解析content-type - application/json的请求
app.use(express.json())
// 解析内容类型为- application/x-www-form-urlencoded的请求
app.use(express.urlencoded({ extended: false }))
app.use(cors())

// 验证token
app.use(expressJWT.expressjwt({ secret: sysConfig.secret, algorithms: ["HS256"] }).unless({ path: routeConfig.whitePath }))
// 验证权限
app.use((req, res, next) => {
  filterVerify(req, res, next)
})
// 错误中间件验证
app.use((err, req, res, next) => {
  console.log('##err####', err)
  errorVerify(err, res)
})
require("./routes/index.js")(app);
app.get("/", (req, res) => {
  res.send(`<h1>欢迎访问！</hr>`)
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))